package com.tenglong.teacher.ui.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.cuotibao.zhuoxiao.basics.utils.JsonUtils;
import com.tenglong.teacher.data.resultInfo.AllAddressInfo;
import com.tenglong.teacher.ui.construction.contract.UserDetailContract;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JsonPareseAddress {
    private static List<String> options1Items;//省份
    private static ArrayList<ArrayList<String>> options2Items;//城市
    private static ArrayList<ArrayList<ArrayList<String>>> options3Items;//区域
    private static ArrayList<AllAddressInfo> allItems;//区域
    private static UserDetailContract.AddressCallback callback;
    private static Handler handler;

    public static synchronized void parseAddress(final Context context, final UserDetailContract.AddressCallback mCallback) {
        callback = mCallback;
        if (handler == null) {
            handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    if (callback != null) {
                        callback.onAddress(options1Items, options2Items, options3Items);
                    }
                }
            };
        }
        if (options1Items == null) {
            options1Items = new ArrayList<>();
        }
        if (options2Items == null) {
            options2Items = new ArrayList<>();
        }
        if (options3Items == null) {
            options3Items = new ArrayList<>();
        }
        if (allItems == null) {
            allItems = new ArrayList<>();
        }
        if (options1Items.size() != 0 && options2Items.size() != 0 && options3Items.size() != 0) {
            if (callback != null) {
                callback.onAddress(options1Items, options2Items, options3Items);
            }
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                parseDetailInfo(context);
            }
        }).start();
    }


    private static void parseDetailInfo(Context context) {
        List<AllAddressInfo> allAddress = Arrays.asList(JsonUtils.fromJson(readFromAsset(context, "address.json"),
                AllAddressInfo[].class));
        allItems.addAll(allAddress);
        for (int i = 0; i < allAddress.size(); i++) {
            ArrayList<String> cityList = new ArrayList<>();
            ArrayList<ArrayList<String>> provinceAreaList = new ArrayList<>();
            options1Items.add(allAddress.get(i).province);
            for (int j = 0; j < allAddress.get(i).cityList.size(); j++) {
                String cityName = allAddress.get(i).cityList.get(j).city;
                cityList.add(cityName);
                ArrayList<String> cityAreaList = new ArrayList<>();
                if (allAddress.get(i).cityList.get(j).areaList != null &&
                        allAddress.get(i).cityList.get(j).areaList.size() > 0) {
                    for (int z = 0; z < allAddress.get(i).cityList.get(j).areaList.size(); z++) {
                        cityAreaList.add(allAddress.get(i).cityList.get(j).areaList.get(z).area);
                    }
                } else {
                    cityAreaList.add("");
                }
                provinceAreaList.add(cityAreaList);
            }
            options2Items.add(cityList);
            options3Items.add(provinceAreaList);
        }
        if (handler != null) {
            handler.sendEmptyMessage(1);
        }
    }


    public static void onDetach() {
        options1Items = null;
        options2Items = null;
        options3Items = null;
        allItems = null;
        callback = null;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

    public static String readFromAsset(Context context, String fileName) {
        String val = null;
        InputStream is = null;
        try {
            is = context.getAssets().open("data/" + fileName);
            val = readStringFromStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                    is = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return val;
    }

    public static String readStringFromStream(InputStream is) throws IOException {

        return new String(readBytesFromStream(is));
    }

    public static byte[] readBytesFromStream(InputStream is) throws IOException {
        byte[] bytes = new byte[is.available()];
        is.read(bytes, 0, bytes.length);
        is.close();
        return bytes;
    }
}
